package com.jweb.framework.http.support;

import javax.servlet.http.HttpServletRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UrlPathHelper;
import org.springframework.web.util.WebUtils;
import com.jweb.framework.core.channel.MessagesProvider;
import com.jweb.framework.http.MainServlet;
import com.jweb.framework.http.RefererResolver;

/**
 * 修改人： Thomas
 * 修改内容：新增 
 * 类说明： HTTP渠道工具类
 */
public class Utils {
	
private static final UrlPathHelper urlPathHelper = new UrlPathHelper();
	
	public static UrlPathHelper getPathHelper(){
		return urlPathHelper ;
	}
	
	public static String getReferer(HttpServletRequest request){
		
		String str = urlPathHelper.getOriginatingQueryString(request);
		if(StringUtils.hasText(str)){
			return urlPathHelper.getPathWithinApplication(request)+"?"+str ;
		}
		
		return urlPathHelper.getPathWithinApplication(request);
	}
	
	public static String getPathInServlet(HttpServletRequest request){
		return urlPathHelper.getLookupPathForRequest(request); // /httpJson.json/smsmopush
	}
	
	public static String getIdentity(HttpServletRequest request){
		String str1 = urlPathHelper.getRequestUri(request); //str1="/weather/fileUpload.do"
		
		int index = str1.indexOf("smsmopush");
		if(index > 5){
			str1 = str1.substring(0, index-1);
		}
		
		String str2 = WebUtils.extractFullFilenameFromUrlPath(str1); // str2="fileUpload.do"
		return StringUtils.stripFilenameExtension(str2);
	}
	
	public static MessagesProvider getMessageProvider(HttpServletRequest request){
		return (MessagesProvider) request.getAttribute(MainServlet.MESSAGES_PROVIDER_ATTRIBUTE);
	}
	
	public static RefererResolver getRefererResolver(HttpServletRequest request){
		return (RefererResolver) request.getAttribute(MainServlet.REFERER_RESOLVER_ATTRIBUTE);
	}
	
	
}
